package com.fyyg.handler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;



/**
 * 捕获异常
 * @author zdx
 *
 */
public class ExceptionResolver implements HandlerExceptionResolver {

	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object obj, Exception exception) {
		//记录日志
		Logger logger=Logger.getLogger(exception.getClass());
		String errorInfo="";
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		exception.printStackTrace(new PrintStream(baos));
		try {
			baos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		errorInfo=baos.toString();
		errorInfo="\n异常拦截器拦截到异常："+exception.getClass().getName()+"\n异常详细信息：\n"+errorInfo;
		logger.error(errorInfo);
		
		/*String url=request.getRequestURI();
		if(url.toLowerCase().contains("ajax.action"))
		{
			try {
				ResponseUtil.renderText(response, "非常抱歉！系统发生了错误，请“返回”并重试");
			} catch (IOException e) {
				logger.error(e);
			}
			return null;
			
		}*/
		
		//跳转
		return new ModelAndView("forward:/error/error.html");
	}

}
